package com.zhike.common.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zhike.common.enums.ResultCode;
import com.zhike.common.utils.TypeChecker;
import com.zhike.common.vo.MyRecord;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @description: 统一返回结果
 */
@Data
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "响应码")
    private Integer code;
    @ApiModelProperty(value = "响应说明")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Object data;

    private R() {

    }

    private R(T data) {
        this.code = ResultCode.SUCCESS.code();
        this.data = data;
    }

    public R(ResultCode resultCode, Object data) {
        this.code = resultCode.code();
        this.message = resultCode.message();
        this.data = data;
    }

    private void setResultCode(ResultCode resultCode) {
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    public R<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public Integer getCode() {
        return this.code;
    }

    public R<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public String getMessage() {
        return this.message;
    }

    public static <T> R<T> success() {
        R<T> r = new R<T>();
        r.setResultCode(ResultCode.SUCCESS);
        return r;
    }

    public static <T> R<T> success(Object data) {
        R<T> r = new R<T>();
        if (TypeChecker.isType(data, Boolean.class)) {
            if ((Boolean) data) {
                r.setResultCode(ResultCode.SUCCESS);
            } else {
                r = new R(data);
            }
        } else {
            r.setResultCode(ResultCode.SUCCESS);
        }
        r.setData(data);
        return r;
    }

    /**
     * 成功返回结果
     *
     * @param record
     * @return
     */
    public static R<Map<String, Object>> success(MyRecord record) {
        return new R<>(record.getColumns());
    }

    /**
     * 成功返回结果
     *
     * @param recordList
     * @return
     */
    public static R<List<Map<String, Object>>> success(List<MyRecord> recordList) {
        List<Map<String, Object>> list = new ArrayList<>();
        recordList.forEach(i -> {
            list.add(i.getColumns());
        });
        return new R<>(list);
    }

    public static <T> R<T> fail() {
        R<T> r = new R<T>();
        r.setResultCode(ResultCode.FAIL);
        r.setData(false);
        return r;
    }

    public static <T> R<T> fail(String msg) {
        R<T> r = new R<T>();
        r.setResultCode(ResultCode.FAIL);
        r.setData(false);
        r.setMessage(msg);
        return r;
    }

    /**
     * 错误码和信息 在枚举类型中声明
     *
     * @param <T>
     * @param resultCode
     * @return
     */
    public static <T> R<T> rsp(ResultCode resultCode) {
        R<T> r = new R<T>();
        r.setResultCode(resultCode);
        r.setData("");
        return r;
    }

    // public static <T> R<T> fail(Integer code, String message) {
    // R<T> r = new R<T>();
    // r.setCode(code);
    // r.setMessage(message);
    // r.setData("");
    // return r;
    // }
}
